package com.liupf.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.liupf.common.utils.TypeConstant;
import com.liupf.domain.BUserEnroll;
import com.liupf.domain.City;
import com.liupf.domain.CompanyPrice;
import com.liupf.domain.DeliveryPrice;
import com.liupf.domain.ParkPrice;
import com.liupf.domain.Terminal;

public class NewOrderController extends Controller{

	public void addNewPark(){
		List<City> citys = City.dao.find("select * from b_city");
		
		setAttr("orderCategoryType", TypeConstant.OrderType.PARK.name());
		setAttr("citys", citys);
		render("order_park_add.ftl");
	}
	
	public void loadCityParks(){
		int cityId = getParaToInt();
		List<Terminal> terminals = Terminal.dao.find("select * from b_terminal where city_id=?",cityId);
		setAttr("terminals", terminals);
		renderJson();
	}
	
	public void cityList(){
		Page<City> page = City.dao.paginate(getParaToInt(0, 1), 20,
				"select * ",
				"from b_city order by id desc");
		List<City> list=new ArrayList<City>();
        for (City city : page.getList()) {
            City temp=city;
            String flag=city.getStr("post");
            String[] flags=flag.split(";");
            String flagName="";
            for (String string : flags) {
                if("0".equals(string)){
                    flagName=flagName+"酒后代驾;";
                }
                if("1".equals(string)){
                    flagName=flagName+"同城配送;";
                }
                if("2".equals(string)){
                    flagName=flagName+"保镖特卫;";
                }
                if("3".equals(string)){
                    flagName=flagName+"代客泊车;";
                }
            }
            temp.put("post", flagName);
            list.add(temp);
        }
        
        Page<City> newPage=new Page<City>(list, page.getPageNumber(), page.getPageSize(), page.getTotalPage(), page.getTotalRow());
        setAttr("page", newPage);
		
		render("city_index.ftl");
	}
	
	public void deleteCity(){
		int id = getParaToInt(0);
		Db.update("delete from b_city where id=?", id);
		redirect("/newOrder/cityList");
	}
	
	public void saveCity(){
		City city = getModel(City.class);
		city.save();
		redirect("/newOrder/cityList");
	}
	
	public void updateCity(){
		City city = getModel(City.class);
		String[] flags=getParaValues("post");
        String flag=StringUtils.join(flags, ";");
        city.set("post", flag);
		city.update();
		redirect("/newOrder/cityList");
	}
	
	public void editCity(){
		setAttr("city", City.dao.findById(getParaToInt()));
		render("city_edit.ftl");
	}
	
	public void addCity(){
		render("city_add.ftl");
	}
	
	public void terminalList(){
		List<Terminal> terminalList = Terminal.dao.find("select * from b_terminal where city_id= ? order by id desc", getParaToInt());
		setAttr("terminalList", terminalList);
		setAttr("cityId", getParaToInt());
		render("terminal_index.ftl");
	}
	
	public void deleteTerminal(){
		int id = getParaToInt(0);
		Db.update("delete from b_terminal where id=?", id);
		
		redirect("/newOrder/terminalList/"+getPara("cityId"));
	}
	
	public void editTerminal(){
		setAttr("terminal", Terminal.dao.findById(getParaToInt()));
		render("terminal_edit.ftl");
	}
	
	public void addTerminal(){
		setAttr("cityId", getParaToInt());
		render("terminal_add.ftl");
	}
	
	public void saveTerminal(){
		Terminal terminal = getModel(Terminal.class);
		terminal.save();
		redirect("/newOrder/terminalList/"+terminal.getInt("city_id"));
	}
	
	public void updateTerminal(){
		Terminal terminal = getModel(Terminal.class);
		terminal.update();
		
		Terminal terminal2 = Terminal.dao.findById(terminal.get("id"));
		redirect("/newOrder/terminalList/"+terminal2.getInt("city_id"));
	}
	
	
	public void deliveryPriceList(){
		List<DeliveryPrice> deliveryPriceList = DeliveryPrice.dao.find("select * from b_delivery_price where city_id= ? order by delivery_id desc", getParaToInt());
		setAttr("deliveryPriceList", deliveryPriceList);
		setAttr("cityId", getParaToInt());
		render("deliveryPrice_index.ftl");
	}
	
	public void deleteDeliveryPrice(){
		int id = getParaToInt(0);
		Db.update("delete from b_delivery_price where delivery_id=?", id);
		
		redirect("/newOrder/deliveryPriceList/"+getPara("cityId"));
	}
	
	public void addDeliveryPrice(){
		setAttr("cityId", getParaToInt());
		render("deliveryPrice_add.ftl");
	}
	
	public void saveDeliveryPrice(){
		DeliveryPrice deliveryPrice = getModel(DeliveryPrice.class);
		deliveryPrice.save();
		redirect("/newOrder/deliveryPriceList/"+deliveryPrice.getInt("city_id"));
	}
	
	public void editDeliveryPrice(){
		setAttr("deliveryPrice", DeliveryPrice.dao.findById(getParaToInt()));
		render("deliveryPrice_edit.ftl");
	}
	
	public void updateDeliveryPrice(){
		DeliveryPrice deliveryPrice = getModel(DeliveryPrice.class);
		deliveryPrice.update();
		
		DeliveryPrice deliveryPrice2 = DeliveryPrice.dao.findById(deliveryPrice.get("delivery_id"));
		redirect("/newOrder/deliveryPriceList/"+deliveryPrice2.getInt("city_id"));
	}
	
	public void companyPriceList(){
		List<CompanyPrice> companyPriceList = CompanyPrice.dao.find("select * from tel_company_price where city_id= ? order by id desc", getParaToInt());
		setAttr("companyPriceList", companyPriceList);
		setAttr("cityId", getParaToInt());
		render("companyPrice_index.ftl");
	}
	
	public void deleteCompanyPrice(){
		int id = getParaToInt(0);
		Db.update("delete from tel_company_price where id=?", id);
		
		redirect("/newOrder/companyPriceList/"+getPara("cityId"));
	}
	
	public void addCompanyPrice(){
		setAttr("cityId", getParaToInt());
		render("companyPrice_add.ftl");
	}
	
	public void saveCompanyPrice(){
		CompanyPrice companyPrice = getModel(CompanyPrice.class);
		
		String startTime = getPara("startTime");
		String startMinutes = getPara("startMinutes");
		String endTime = getPara("endTime");
		String endMinutes = getPara("endMinutes");
		
		String startRunTime = startTime + ":" + startMinutes + ":00";
		String endRunTime = endTime + ":" + endMinutes + ":59";
		
		companyPrice.put("startTime", startRunTime);
		companyPrice.put("endTime", endRunTime);
		
		companyPrice.save();
		redirect("/newOrder/companyPriceList/"+companyPrice.getInt("city_id"));
	}
	
	public void parkPriceIndex() {
		int cityId = getParaToInt(0);
		String sql = "select * from b_park_price where city_id=?";
		List<Record> list = Db.find(sql, cityId);
		setAttr("parkPriceList", list);
		
		setAttr("cityId", cityId);
		render("park_price_index.ftl");
	}
	
	public void saveParkPrice(){
		String cityId = getPara("cityId");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		String price = getPara("price");
		
		ParkPrice parkPrice = new ParkPrice();
		parkPrice.put("start_time", startTime);
		parkPrice.put("end_time", endTime);
		parkPrice.put("price", price);
		parkPrice.put("city_id", cityId);
		
		parkPrice.save();
		
		redirect("/newOrder/parkPriceIndex/"+cityId);
	}
	
	public void deleteParkPrice(){
		Integer id = getParaToInt(0);
		
		ParkPrice parkPrice = ParkPrice.dao.findById(id);
		ParkPrice.dao.deleteById(id);
		
		redirect("/newOrder/parkPriceIndex/"+parkPrice.getInt("city_id"));
	}
}
